package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.AddressListBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

import java.util.List;


/*
    地址修改
 */

public class AddressListAvtivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private ImageView regist_back;
    private TextView tv_headtitle,tv_addr;
    private MyToast myToast;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private final String MY_MODULE_UPDATE_ADDRESS_FLAG="SERVICE_ADDR_LIST";
    private final String My_MODULEDELEETEFLAG="My_MODULEDELEETEFLAG";
    private final String MY_MODULEUPDATEFLAG="MY_MODULEUPDATEFLAG";
    private ListView myListView;
    private String uid;
    private List<AddressListBean> listData;
    private AddressListAdapter addressListAdapter;
    private AddressListBean addressListBeanRemove;
    private int positionflag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_layout);
        context=this;
        myToast=new MyToast(context);
        initView();
        contributorPresenter = new ContributorPresenter(this, context);
        uid=Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
        showData();
    }

    private void showData()
    {
        contributorPresenter.getAddrList(uid,MY_MODULE_UPDATE_ADDRESS_FLAG);
    }

    private void initView()
    {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("预约地址");
        tv_addr=(TextView) findViewById(R.id.tv_addr);
        tv_addr.setOnClickListener(this);
        myListView=(ListView) findViewById(R.id.lv_list);
        myListView.setDivider(null);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //获取列表
            if (flag.equals(MY_MODULE_UPDATE_ADDRESS_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    String message=JsonTools.objectToJsonString(respObj.getMessage());
                    listData=JsonTools.jsonStringToList(message,AddressListBean.class);
                    if(listData!=null &&listData.size()>0)
                    {
                        addressListAdapter=new AddressListAdapter(context,listData);
                        myListView.setAdapter(addressListAdapter);
                    }
                }
            }
            //删除列表
            else if(flag.equals(My_MODULEDELEETEFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    if(listData.contains(addressListBeanRemove))
                    {
                        listData.remove(addressListBeanRemove);
                        addressListAdapter.notifyDataSetChanged();
                    }
                }else {
                    myToast.showShort("刪除失败");
                }
            }
            //更新列表
            else if(flag.equals(MY_MODULEUPDATEFLAG))
            {

                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {

                    for(int i=0;i<addressListAdapter.getCount();i++)
                    {
                        if(i==positionflag)
                        {
                            listData.get(i).setType(1);
                        }else
                        {
                            listData.get(i).setType(0);
                        }
                    }
                    addressListAdapter.notifyDataSetChanged();
                }else {
                    myToast.showShort("更改失败");
                }

            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_addr:
                Intent intent_change_arr = new Intent(context, ChangeAddressAvtivity.class);
                intent_change_arr.putExtra("headtitle","添加地址");
                startActivityForResult(intent_change_arr,0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0&& resultCode==1)
        {
            showData();
        }
    }

    class AddressListAdapter extends BaseAdapter {

        private Context context;
        private List<AddressListBean> messageBeen;
        private LayoutInflater mInflater;

        public AddressListAdapter(Context context, List<AddressListBean> messageBeen) {
            this.context = context;
            this.messageBeen = messageBeen;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return messageBeen.size();
        }

        @Override
        public Object getItem(int position) {
            return messageBeen.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_address, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            bindListItem(holder, position);//控制视图显示数据

            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addressListBeanRemove=(AddressListBean) getItem(position);
                    contributorPresenter.getDelAddress(uid,addressListBeanRemove.getId(),My_MODULEDELEETEFLAG);
                }
            });

            holder.tv_setting.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    positionflag=position;
                    addressListBeanRemove=(AddressListBean) getItem(position);
                    contributorPresenter.getAddrUpdate(uid,addressListBeanRemove.getId(),addressListBeanRemove.getName(),
                            addressListBeanRemove.getPhone(),addressListBeanRemove.getAddressHead(),addressListBeanRemove.getAddressFoot(),1,MY_MODULEUPDATEFLAG);
                }
            });

            holder.tv_address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,ChangeAddressAvtivity.class);
                    intent.putExtra("headtitle","修改地址");
                    intent.putExtra("object",JsonTools.objectToJsonString((AddressListBean) getItem(position)));
                    startActivityForResult(intent,0);

                }
            });

            holder.iv_go.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,ChangeAddressAvtivity.class);
                    intent.putExtra("headtitle","修改地址");
                    intent.putExtra("object",JsonTools.objectToJsonString((AddressListBean) getItem(position)));
                    startActivityForResult(intent,0);
                }
            });

            return convertView;
        }

        //控制视图显示数据
        private void bindListItem(ViewHolder holder, int pos) {
            try {
                AddressListBean addressListBean=messageBeen.get(pos);
                holder.tv_name.setText(addressListBean.getName());
                holder.tv_phonenumber.setText(addressListBean.getPhone());
                holder.tv_address.setText(addressListBean.getAddressHead()+addressListBean.getAddressFoot());
                if(addressListBean.getType()==0)
                {
                    holder.tv_setting.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.shape),null,null,null);
                }else {
                    holder.tv_setting.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.shapeselect),null,null,null);
                }

            }catch (Exception e)
            {

            }

        }

        public class ViewHolder {
            private TextView tv_name;
            private TextView tv_phonenumber;
            private TextView tv_address;
            private TextView tv_setting;
            private TextView tv_delete;
            private ImageView iv_go;

            public ViewHolder(View view) {
                iv_go=(ImageView) view.findViewById(R.id.iv_go);
                tv_name = (TextView) view.findViewById(R.id.tv_name);
                tv_phonenumber = (TextView) view.findViewById(R.id.tv_phonenumber);
                tv_address = (TextView) view.findViewById(R.id.tv_address);
                tv_setting = (TextView) view.findViewById(R.id.tv_setting);
                tv_delete=(TextView) view.findViewById(R.id.tv_delete);
            }
        }

    }

}
